﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;

namespace LiWi
{
    public partial class 自定义指令封装 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        int[] axisList = { 0, 1, 2, 3 };
        int startMode;
        List<int> startNum = new List<int>();

        public 自定义指令封装()
        {
            InitializeComponent();
            adapt.SetForm(this);
        }


        /// <summary>
        /// 读取轴 0，轴 1，轴 2 的命令位置
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="iaxislist">轴列表</param>
        public void Demo_getPos(IntPtr handle, int[] iaxislist)
        {
            float[] dposvalue = new float[axisList.Length];
            var log = "";
            string cmdbuff = "";
            string tempbuff = "";
            byte[] cmdbuffAck = new byte[20480];
            if (iaxislist == null)
            {
                MessageBox.Show("【获取位置】参数输入有误");
                return;
            }
            foreach (int item in iaxislist)
            {
                //PMC_GetDpos( handle, i,ref dposvalue[i]);
                //tempbuff = string.Format("PMC_GetDpos({0},{1}, ref {2}),", handle, i, dposvalue[i]);
                tempbuff = string.Format("?DPOS({0})\n", item);
                cmdbuff += tempbuff;
                if (cmdbuff.Length > 1000)
                {
                    MessageBox.Show("【位置获取】字符串拼接过长");
                    return;
                }
            }
            var ret=0;
            if (startMode == 1)
            {
                ret = PMC_Command(_handle, cmdbuff, cmdbuffAck, 2048);
            }
            else if (startMode == 2)
            {
                ret = PMC_Execute(_handle, cmdbuff, cmdbuffAck, 2048);
            }
            if (ret != 0)
            {
                MessageBox.Show("【位置获取】指令执行失败");
                return;
            }

            //List<float> ftempbuff = new List<float>(axisList.Length);
            //for (int i = 0; i < axisList.Length; i++)
            //{
            //    ftempbuff[i] = float.Parse(cmdbuffAck[i].ToString());// 将 cmdbuffAck中的每个值转换为浮点数并存储在 ftempbuff 中
            //}

            txtlogShow.AppendText($"startMode={startMode},当前指令字符串为{cmdbuff}\r\n");
        }






        //        假设控制一个 XY 两轴平台，从坐标点（0,0）—>（100,0）（输出口 0 输出 50ms）
        //—>（100,100）（输出口 0 输出 50ms）—>（0,100）（输出口 0 输出 50ms）—>（0,0）
        //（输出口 0 输出 50ms）的轨迹，则可以通过自己封装，用一条函数，快速发送下去。

        public void Demo_MoveAbs(IntPtr handle, int iMoveLen,int iaxisNum, int[] iaxislist, float[] fposlist, int iout, int[] outlist, int outtime)
        {
            string cmdbuff;
            string tempbuff;
            byte[] cmdbuffAck=new byte[20480];
            int ret = 0;
            
            //先读取直线缓冲
            int iBuffLen = 0;
            ret = PMC_GetRemain_LineBuffer(handle, iaxislist[0], ref iBuffLen);
            if (iBuffLen<=iMoveLen*2)
            {
                MessageBox.Show("运动缓冲不够");
                return;
            }
            //生成命令
            cmdbuff = "BASE(";
            for (int i = 0; i < iaxisNum ; i++)
            {
                if (i<iaxisNum-1)
                {
                    tempbuff = string.Format("{0},", iaxislist[i]);
                    cmdbuff += tempbuff;
                }
                else
                {
                    tempbuff = string.Format("{0})\n", iaxislist[i]);
                    cmdbuff += tempbuff;
                }
                
                if (cmdbuff.Length > 1000)
                {
                    MessageBox.Show("【Demo_MoveAbs】指令拼接过长");
                    return;
                }
            }
                for (int i = 0; i < iMoveLen; i++)
                {
                    if (outlist[i] == 0)  //无输出信号
                    {
                        cmdbuff += "MoveAbs(";
                        tempbuff = string.Format("{0},{1})\n", fposlist[i * iaxisNum], fposlist[i * iaxisNum + 1]);
                        cmdbuff += tempbuff;
                    }
                    else if (outlist[i] == 1)
                    {
                        cmdbuff += "MoveAbs(";
                        tempbuff = string.Format("{0},{1})\n", fposlist[i * iaxisNum], fposlist[i * iaxisNum + 1]);
                        cmdbuff += tempbuff;
                        cmdbuff += "Move_op2(";
                        tempbuff = string.Format("{0},{1},{2})\n", iout, 1, outtime);
                        cmdbuff += tempbuff;
                    }
                    else
                    {
                        MessageBox.Show("参数错误");
                        return;
                    }
                   

                }
                if (cmdbuff.Length > 1000)
                {
                    MessageBox.Show("拼接过长");
                    
                }
            if (startMode == 1)
            {
                ret = PMC_Command(handle, cmdbuff, cmdbuffAck, 2048);
            }
            else if (startMode == 2)
            {
                PMC_Execute(handle, cmdbuff, cmdbuffAck, 2048);
            }
            
            txtlogShow.AppendText($"startMode={startMode},当前指令字符串为{cmdbuff}\r\n");
        }




        /// <summary>
        /// 添加指令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndiy_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                if (combcom.SelectedIndex == 0)
                {

                    startNum.Add(0);
                    txtrun.AppendText("=》获取坐标");
                }
                else if (combcom.SelectedIndex == 1)
                {

                    startNum.Add(1);
                    txtrun.AppendText("=》2号封装");
                }
            }
            else MessageBox.Show("请先连接控制器");
        }

        private void btnstar_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                for (int i = 0; i < axisList.Length; i++)
                {
                    PMC_SetAtype(_handle, i, 1);
                    PMC_SetSpeed(_handle, i, Convert.ToSingle(num_sp.Value));
                    PMC_SetUnits(_handle, i, Convert.ToSingle(num_uint.Value));
                    PMC_SetAccel(_handle, i, Convert.ToSingle(num_accel.Value));
                    PMC_SetDecel(_handle, i, Convert.ToSingle(num_decel.Value)); 
                }
                startMode = 1;
                if (startNum.Count > 0)
                {
                    foreach (int item in startNum)
                    {
                        if (item == 0)
                        {
                            Demo_getPos(_handle, axisList);
                        }
                        else if (item == 1)
                        {
                            int[] axis = new int[2] { 0, 1 };
                            float[] POS = new float[10] { 0, 0, 0, 100, 100, 100, 100, 0, 0, 0 };
                            int[] otlist = new int[5] { 0, 1, 1, 1, 1 };
                            Demo_MoveAbs(_handle, 5, 2, axis, POS, 2, otlist, 50);
                        }
                    }
                }
                else MessageBox.Show("请先添加指令");
            }
            else MessageBox.Show("请先连接控制卡");
        }

        private void btnstarexe_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                startMode = 2;
                if (startNum.Count > 0)
                {
                    foreach (int item in startNum)
                    {
                        if (item == 0)
                        {
                            Demo_getPos(_handle, axisList);
                        }
                        else if (item == 1)
                        {
                            int[] axis = new int[2] { 0, 1 };
                            float[] POS = new float[10] { 0, 0, 0, 100, 100, 100, 100, 0, 0, 0 };
                            int[] otlist = new int[5] { 0, 1, 1, 1, 1 };
                            Demo_MoveAbs(_handle, 5, 2, axis, POS, 2, otlist, 50);
                        }
                    }
                }
                else MessageBox.Show("请先添加指令");
            }
            else MessageBox.Show("请先连接控制卡");
        }


    }
}
